
//? 10 -> 2 整数和小数不一样 整数除 小数乘
//? 2 -> 10 转换整数部分从尾到头开始累加咯 转换小数部分需要将小数点后面的每一位乘以2的负次幂(2^-1,2^-2)，然后将它们相加
function twoten(num) {

    const int = String(num).split('.')[0]
    const dec = String(num).split('.')[1]

    return i(int) + '.' + d(dec)
}
function i(num) {
    let number = Number(num)
    let res = ''
    while (number >= 1) {
        //? 从右向左开始加
        res = number % 2 + res
        number = Math.floor(number / 2)
    }
    return res
}
function d(num) {
    let res = ''
    let number = Number(0 + '.' + num)
    while (number.toString().indexOf('.') !== -1 && res.length < 46) {
        let ji = number * 2
        //? 从左向右开始加
        res += Math.floor(ji)
        if (Math.floor(ji) === 1) {
            ji -= 1
        }
        number = ji
    }
    return res
}
console.log(twoten(123.234));
//? 1111011.0011101111100111011011001000101101000011100101
function tentwo(num) {

    const [head,tail] = String(num).split('.')
    return ii(head) + dd(tail)
}
function ii(str) {
    let res = 0
    let count = 0
    for (let i = str.length - 1; i >= 0; i --) {
        res += +str[i] * Math.pow(2, count)
        count++
    }
    return res

}
function dd(str) {
    let res = 0
    let count = -1
    for (let i = 0; i < str.length; i ++) {
        res += +str[i] * Math.pow(2,count)
        count--
    }
    return Number('0' + res)
}
console.log(tentwo(1111011.0011101111100111011011001000101101000011100101))
